package com.magic.gre.base.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.classic.common.MultipleStatusView;
import com.magic.gre.base.dialog.BaseDialog;
import com.magic.gre.base.dialog.LoadingDialog;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.ui.dialog.LoginHintDialog;
import com.noname.lib_base_java.entity.NetFielBean;
import com.noname.lib_base_java.mvp.BaseContract;
import com.noname.lib_base_java.net.download.DownloadInfo;
import com.noname.lib_base_java.util.L;
import com.noname.lib_base_java.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2018-12-11 16:39
 * 文件名: BaseMVPFragment
 * 描述:
 */
public abstract class BaseMVPFragment<P extends BaseContract.BasePresenter> extends BaseFragment
        implements BaseContract.BaseView, OnRefreshListener, OnLoadMoreListener {

    protected P mPresenter = creatPresenter();
    //是否有loading任务
    private boolean hasLoading = false;
    protected SmartRefreshLayout mSmartRefreshLayout;
    protected MultipleStatusView mMultipleStatusView;

    /**
     * 视图是否加载完毕
     */
    private boolean isViewPrepar = false;

    /**
     * 数据是否加载过了
     */
    private boolean hasLoadData = false;

    private LoadingDialog loadingDialog;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            lazyLoadDataIfPrepared();
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewPrepar = true;
        lazyLoadDataIfPrepared();
    }

    private void lazyLoadDataIfPrepared() {
        if (getUserVisibleHint() && isViewPrepar && !hasLoadData) {
            lazyLoad();
            hasLoadData = true;
        }
    }

    @Override
    public void showContent() {
        if (mMultipleStatusView != null) {
            mMultipleStatusView.showContent();
        }
    }

    @Override
    public void showEmpty() {
        if (mMultipleStatusView != null) {
            mMultipleStatusView.showEmpty();
        }
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mSmartRefreshLayout = view.findViewById(com.noname.lib_base_java.R.id.mSmartRefreshLayout);
        mMultipleStatusView = view.findViewById(com.noname.lib_base_java.R.id.mMultipleStatusView);
        if (mSmartRefreshLayout != null) {
            mSmartRefreshLayout.setOnLoadMoreListener(this);
            mSmartRefreshLayout.setOnRefreshListener(this);
        }
        if (mMultipleStatusView != null) {
            mMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    netOverride();
                }
            });
        }
    }

    /**
     * 未登录
     */
    @Override
    public void onNoLogin() {
        Apphelper.removeAll();
        synchronized (LoginHintDialog.class) {
            if (!getLoginDialog().isResumed()) {
               showLoglinDialog();
            }
        }
    }

    /**
     * 文件下载完成并已经写入了本地
     */
    @Override
    public void vDownload(DownloadInfo info) {
//        if (b){
//            ToastUtil.getInstance().showNormal(mContext,"下载成功");
//        }else {
//            ToastUtil.getInstance().showNormal(mContext,"下载失败");
//        }
    }

    @Override
    public void vDownloadFail(String msg) {

    }

    /**
     * 网络错误
     */
    @Override
    public void onNetError(String msg) {

        hideLoadDialog();

        if (mSmartRefreshLayout != null) {
            stopRefresh(false, false);
        }
        if (mMultipleStatusView != null) {
            mMultipleStatusView.showError();
        }
    }

    /**
     * 显示提示语句
     *
     * @param msg
     */
    @Override
    public void doPrompt(String msg) {
        synchronized (BaseMVPFragment.class) {
            ToastUtil.getInstance().showNormal(mContext, msg);
        }
    }

    /**
     * 显示加载提示弹窗
     */
    @Override
    public void showLoadDialog() {
        synchronized (BaseMVPFragment.class) {
            if (!hasLoading) {
                hasLoading = true;
                loadingDialog = new LoadingDialog();
                loadingDialog.showThis(getChildFragmentManager(), LoadingDialog.class.getSimpleName());
            }
        }
    }

    /**
     * 隐藏加载提示弹窗
     */
    @Override
    public void hideLoadDialog() {
        synchronized (BaseMVPFragment.class) {
            if (loadingDialog != null) {
                loadingDialog.dismissThis(loadingDialog.isResumed());
            }
        }
    }

    @Override
    public void upLoadFiels(List<NetFielBean> fielBeens) {
        ToastUtil.getInstance().showNormal(mContext, "文件上传成功");
        hideLoadDialog();
    }

    @Override
    public void upLoadFielsFail(String failMsg) {
        ToastUtil.getInstance().showNormal(mContext, "文件上传失败");
        hideLoadDialog();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    protected void stopRefresh(boolean refreshSuc, boolean isLoadMore) {
        L.e("LLSSQQ", "stopRefresh");
        if (mSmartRefreshLayout != null) {
            if (mSmartRefreshLayout.getState() == RefreshState.Refreshing) {
                mSmartRefreshLayout.finishRefresh(refreshSuc);
            }

            if (isLoadMore) {
                mSmartRefreshLayout.finishLoadMore().setNoMoreData(false);
            } else {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    protected abstract P creatPresenter();

    protected abstract void lazyLoad();

    protected abstract void netOverride();

}
